package com.sunrise.xdoc.view.catalog;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import net.sf.jasperreports.engine.JRException;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.builders.FastReportBuilder;

import com.sunrise.xdoc.constant.ReportFileTypeEnum;
import com.sunrise.xdoc.entity.catalog.LoaiSanPham;
import com.sunrise.xdoc.entity.catalog.SanPham;
import com.sunrise.xdoc.entity.config.UploadFile;
import com.sunrise.xdoc.event.EventData;
import com.sunrise.xdoc.event.EventManager;
import com.sunrise.xdoc.event.EventNameConstants;
import com.sunrise.xdoc.helper.ReportHelper;
import com.sunrise.xdoc.service.ICatalogService;
import com.sunrise.xdoc.service.IReportService;
import com.sunrise.xdoc.view.AbstractView;

@ManagedBean
@SessionScoped
public class SanPhamView extends AbstractView<SanPham> {
	private static final long serialVersionUID = 9160550591813254187L;

	@ManagedProperty(value = "#{catalogService}")
	private ICatalogService catalogService;

	@ManagedProperty(value = "#{reportService}")
	private IReportService reportService;

	private List<LoaiSanPham> loaiSPs;

	private String reportUrl;

	@Override
	@PostConstruct
	protected void init() {
		super.init();
		loaiSPs = catalogService.getAllLoaiSanPhams();
	}

	public Long getSelectedLoaiSanPhamId() {
		if (getEditedEntity() != null && getEditedEntity().getLoaiSP() != null) {
			return getEditedEntity().getLoaiSP().getId();
		}
		return null;
	}

	public void setSelectedLoaiSanPhamId(Long selectedLoaiSanPhamId) {
		for (LoaiSanPham lsp : loaiSPs) {
			if (selectedLoaiSanPhamId.equals(lsp.getId())) {
				getEditedEntity().setLoaiSP(lsp);
				return;
			}
		}
		getEditedEntity().setLoaiSP(null);
	}

	public void handleImageUpload(FileUploadEvent event) {
		UploadFile uf = new UploadFile();
		uf.setTitle(event.getFile().getFileName());
		uf.setData(event.getFile().getContents());
		getEditedEntity().setHinhAnh(uf);
	}

	/**
	 * Active/Unactive the entity <br/>
	 * TODO consider move to abstract class later, because it's common action
	 * for some active type entities.
	 */
	public void activeAction(boolean active) {
		// Active on view
		for (SanPham sp : getSelectedEntities()) {
			sp.setActive(active);
		}

		// Update database
		catalogService.activeSanPhams(getSelectedEntityIds(), active);
		EventManager.fireEvent(EventNameConstants.ACTIVE_SP, new EventData(this, getSelectedEntities()));
	}

	@Override
	protected String getDetailPanelViewId() {
		return "sanPhamDetailFormId:sanPhamPanelGridId";
	}

	public ICatalogService getCatalogService() {
		return catalogService;
	}

	public void setCatalogService(ICatalogService catalogService) {
		this.catalogService = catalogService;
	}

	public IReportService getReportService() {
		return reportService;
	}

	public void setReportService(IReportService reportService) {
		this.reportService = reportService;
	}

	public StreamedContent getHinhAnh() {
		if (getEditedEntity() != null && getEditedEntity().getHinhAnh() != null) {
			return new DefaultStreamedContent(new ByteArrayInputStream(getEditedEntity().getHinhAnh().getData()));
		}
		return null;
	}

	public List<LoaiSanPham> getLoaiSPs() {
		return loaiSPs;
	}

	public void setLoaiSPs(List<LoaiSanPham> loaiSPs) {
		this.loaiSPs = loaiSPs;
	}

	@Override
	protected String getDataModelBeanName() {
		return "sanPhamDataModel";
	}

	protected void buildReport(ReportFileTypeEnum fileTypeEnum) throws ClassNotFoundException, FileNotFoundException,
			JRException {
		FastReportBuilder builder = ReportHelper.getReportBuilder();
		builder.setTitle("Công ty cổ phần phần mềm SunriseSoft - SmartCoffee");
		builder.setSubtitle("Danh mục sản phẩm");
		builder.addColumn(ReportHelper.buildSttColumn());
		builder.addColumn("Tên", "name", String.class.getName(), 100, ReportHelper.NAME_STYLE);
		builder.addColumn("Mã", "code", String.class.getName(), 100, ReportHelper.NAME_STYLE);
		builder.addColumn("Giá", "donGia", Long.class.getName(), 100, true, "###,###.#", ReportHelper.NUMBER_STYLE);
		DynamicReport report = builder.build();
		List<SanPham> sanPhams = getDaoHelper().getDao(SanPham.class).loadAll();
		String reportUrl = reportService.printReport(report, sanPhams, fileTypeEnum);
		setReportUrl(reportUrl);
	}

	public void printPdf() throws FileNotFoundException, JRException, ClassNotFoundException {
		buildReport(ReportFileTypeEnum.PDF);
	}

	public void printXls() throws FileNotFoundException, JRException, ClassNotFoundException {
		buildReport(ReportFileTypeEnum.EXCEL);
	}

	public String getReportUrl() {
		return reportUrl;
	}

	public void setReportUrl(String reportUrl) {
		this.reportUrl = reportUrl;
	}
}
